<?PHP 
namespace models;
/**
 * This code is property of Piotr Poradzisz. Do not distribute under threat of legal consequences.
 * All rights reserved.
 * Все права защищены.
 * Alle Rechte vorbehalten.
 * Todos los derechos reservados.
 * Wszelkie prawa zastrzeżone.
 *                  جميع الحقوق محفوظة
 * 保留所有权利
 * すべての著作権
 * @author Piotr Poradzisz
 * email: piotr.poradzisz@gmail.com
 * tel.: +48668027020
 */


class Mysql {

    private $_DbHost;
    
    private $_DbUserName;
    
    private $_DbPassword;
    
    private $_DbName;
    
    function __construct($dbHost = null, $dbUserNamer = null,  $pass = null, $dbName = null) {

        if(!isset($dbName) or !isset($dbUserNamer) or !isset($dbPassword) or !isset($dbHost))
        {
            if(file_exists('mysql.php')) include 'mysql.php';
            else include '../mysql.php';
        }
        
        $this->_DbHost = $dbHost;
        $this->_DbUserName = $dbUserName;
        $this->_DbPassword = $dbPassword;
        $this->_DbName = $dbName;
        $this->PrivMysqlConnect();
    }    
    
    private function PrivMysqlConnect()
    {
        @mysql_connect($this->_DbHost, $this->_DbUserName, $this->_DbPassword) or die ("Nieudane polaczenie z systemem bazy danych pod adresem $this->_DbHost!");
        @mysql_select_db($this->_DbName) or die ("Nieudane polaczenie z baza danych $this->_DbName!");
        mysql_query('SET NAMES utf8');
    }

    public function setQuery($query){

        //Helper::WriteToFile('../cache/mysql.log', $query);
        
        $result = mysql_query($query);
        if (mysql_errno()){
            echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$query\n<br>";
            return false;
        }
        else return $result;
    }

    public function getTable($query, $array = false){
        $result = $this->setQuery($query);
        if($array === true) while($return[] = mysql_fetch_array($result));
        else while($return[] = mysql_fetch_assoc($result));
        unset ($return[count($return)-1]);
        return $return;
    }

    public function getRow($query, $array = false){
        $result = $this->setQuery($query);
        if($array === true) $return = mysql_fetch_array ($result);
        else $return = mysql_fetch_assoc($result);
        return $return;
    }

    public function getRecord($query){
        $result = $this->setQuery($query);
        $return = mysql_fetch_array($result);
        return $return[0];
    }

    public function getInfo($query){
        $result = $this->setQuery($query);
        while($return[] = mysql_fetch_field($result));
        unset ($return[count($return)-1]);
        return $return;
    }
}
?>